.PHONY:all clean

CC=arm-linux-gcc
LD=arm-linux-ld
OBJDIR = obj
TARGET = burnTo2440Nand.bin

SRCDIR += src
SRCDIR += src/common
SRCDIR += src/drv
SRCDIR += src/modules
SRCDIR += src/app/pwmMuscic
SRCDIR += src/core
SRCDIR += src/core/tsk
SRCDIR += src/core/sys

C_FILES = $(foreach i, $(SRCDIR), $(wildcard $(i)/*.c))
OBJS_FROM_C = $(foreach i, $(C_FILES), obj/$(i:.c=.o))
OBJS_FROM_ASM = obj/src/core/start.o obj/src/core/tsk/taskMgrSub.o
OUTDIRS = $(foreach i, $(dir $(C_FILES)), obj/$(i))

INCLUDE = $(SRCDIR)
IINCLUDE = $(foreach i, $(INCLUDE), -I$(i))
C_FLAGS = -Wall -g -c $(IINCLUDE)
LD_FLAGS = -T nand.lds

all:premake $(TARGET)
	@echo 'done.'

premake:
	@echo make premake...
	$(foreach i, $(OUTDIRS), $(shell mkdir -p $(i)))

$(TARGET):$(OBJS_FROM_ASM) $(OBJS_FROM_C)
	@find obj/ -name *.o -exec cp '{}' . \;
	@rm -r obj
	$(LD) $(LD_FLAGS) -o nand.elf *.o
	arm-linux-objcopy -O binary nand.elf $@
	arm-linux-objdump -D -m arm nand.elf > nand.dis
	@rm *.o

$(OBJS_FROM_ASM):src/core/start.S src/core/tsk/taskMgrSub.S
	@$(CC) -c src/core/start.S -o obj/src/core/start.o
	@$(CC) -c src/core/tsk/taskMgrSub.S -o obj/src/core/tsk/taskMgrSub.o

obj/%.o:%.c
	@$(CC) $(C_FLAGS) $< -o $@

clean:
	@-rm -rf ./obj *.o
	@-rm -f $(TARGET) nand.elf nand.dis
	@echo clean done.

